const { no, ok } = require('../../utils/utlis');

const roleService = require('../../services/admin/roleService');
module.exports = {
	// 角色列表(下拉)
	list: async (req, res, next) => {
		try {
			let list = await roleService.list();
			res.send(ok(list));
		} catch (error) {
			res.send(no(error.message));
		}
	},
	listAll: async (req, res, next) => {
		try {
			let { keywords } = req.body;
			let list = await roleService.listAll(keywords);
			res.send(ok(list));
		} catch (error) {
			res.send(no(error.message));
		}
	},
	add: async (req, res, next) => {
		try {
			let { name, routes, halfCheckedKeys, checkedKeys } = req.body;
			await roleService.add(name, routes.join(','), halfCheckedKeys.join(','), checkedKeys.join(','));
			res.send(ok());
		} catch (error) {
			res.send(no(error.message));
		}
	},
	edit: async (req, res, next) => {
		try {
			let { name, routes, halfCheckedKeys, checkedKeys, id } = req.body;
			await roleService.edit(name, routes.join(','), halfCheckedKeys.join(','), checkedKeys.join(','), id);
			res.send(ok());
		} catch (error) {
			res.send(no(error.message));
		}
	},
	del: async (req, res, next) => {
		try {
			let { id } = req.body;
			await roleService.del(id);
			res.send(ok());
		} catch (error) {
			res.send(no(error.message));
		}
	},
	detail: async (req, res, next) => {
		try {
			let { id } = req.body;
			let obj = await roleService.detail(id);
			res.send(
				ok({
					id: obj.id,
					routes: obj.routes.split(','),
					halfCheckedKeys: obj.halfCheckedKeys.split(','),
					checkedKeys: obj.checkedKeys.split(','),
					name: obj.name
				})
			);
		} catch (error) {
			res.send(no(error.message));
		}
	}
};
